package com.weimob.mcloud.wechatwork.customer.service.common;

import com.google.common.collect.Lists;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.response.FollowUserBO;
import com.weimob.mcloud.wechatwork.core.enums.customer.ExternalUserAddWayEnum;
import com.weimob.mcloud.wechatwork.customer.third.PrivateSphereThird;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author yu.liu07
 * @version 1.0
 * @date 2022/9/11 1:24 下午
 */
@Component
@Slf4j
public class CustomerCommonService {

    @Autowired
    private PrivateSphereThird privateSphereThird;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 获取外部联系人手机号信息【仅通过搜索手机号添加方式获取】
     *
     * @param followUser
     */
    public String getCustomerPhone(List<FollowUserBO> followUser) {
        if (CollectionUtils.isEmpty(followUser)) {
            return null;
        }

        List<FollowUserBO> followUserList = followUser.stream().filter(item ->
                ExternalUserAddWayEnum.PHONE_SEARCH.getCode().equals(item.getAddWay())
        ).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(followUserList)) {
            //没有通过手机号搜索添加的 不处理
            return null;
        }

        //只获取第一个手机号
        List<String> remarkMobiles = Lists.newArrayList();
        followUserList.forEach(e -> {
            if (CollectionUtils.isNotEmpty(e.getRemarkMobiles())) {
                remarkMobiles.addAll(e.getRemarkMobiles());
            }
        });
        return CollectionUtils.isEmpty(remarkMobiles) ? null : remarkMobiles.get(0);
    }


    /**
     * 获取外部联系人首次添加事件
     *
     * @param followUserList
     */
    public Date getCustomerFirstAddTime(List<FollowUserBO> followUserList) {
        if (CollectionUtils.isEmpty(followUserList)) {
            return null;
        }
        Long firstAddTime = followUserList.get(0).getCreatetime();
        for (FollowUserBO followUserBO : followUserList) {
            if (firstAddTime > followUserBO.getCreatetime()) {
                firstAddTime = followUserBO.getCreatetime();
            }
        }
        return new Date(firstAddTime * 1000L);
    }


    /**
     * 记录事件时间
     *
     * @param msgUk      msgUk
     * @param createTime 创建时间
     */
    public void recordEventTime(String msgUk, Long createTime) {
        if (Objects.isNull(createTime)) {
            return;
        }
        if (Boolean.FALSE.equals(redisTemplate.hasKey(msgUk))) {
            redisTemplate.opsForValue().set(msgUk, String.valueOf(createTime), 60, TimeUnit.MINUTES);
            return;
        }
        String createTimeStr = redisTemplate.opsForValue().get(msgUk);
        if (StringUtils.isNotBlank(createTimeStr)) {
            Long oldTime = Long.valueOf(createTimeStr);
            if (oldTime.compareTo(createTime) < 0) {
                redisTemplate.opsForValue().set(msgUk, String.valueOf(createTime), 60, TimeUnit.MINUTES);
            }
        }
    }


}
